;
(function() {
	var role = require("./role");
	var BusCon = require("../../core/bus/_con");
	var UserCon = require("../../core/user/_con");
	var GoodsCon = require("../../core/goods/_con");
	var UserCartGoodsCon = require("../../core/user.cart_goods/_con");
	var AchCon = require("../../core/ach/_con");
	var db = require("../../core/db/index");
	var config = require("../../core/_include/config");
	var ResponObj = require("../_include/responObj");

	module.exports = {
		prefix: "/user/cart",
		get: {
			//获取当前登录用户的购物车信息
			"/": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var cart = user.getCart(query.num, query.page);
				this.body = ResponObj("json", cart);
			},
			"/detail": function() {
				//带上商品的信息
				var user = role.getUser(this);
				var query = this.request.query;
				var cartGoods = user.getCartDetail(query.num, query.page);
				this.body = ResponObj("json", cartGoods);
			},
			"/getByBus": function() {
				var user = role.getUser(this);
				var query = this.request.query;
				var bus_id = query.bus_id || role.getBusInfo(this)._id;
				if (bus_id) {
					var cartGoods = user.getCartDetailByBus(bus_id, query.num, query.page);
				} else {
					cartGoods = user.getCartDetail(query.num, query.page);
				}
				this.body = ResponObj("json", cartGoods);
			}
		},
		post: {
			//添加商品到购物车
			"/": function() {
				var user = role.getUser(this);
				var cart_goods_info = user.addGoodsToCart([this.request.body]);
				console.log("cart:", cart_goods_info)
					//返回详细列表
				module.exports.get["/getByBus"].call(this);
			},
			//将购物车商品转化为订单
			"/makeAch": function() {
				var user = role.getUser(this);
				var cart_id_list = this.request.body.cart_id_list;
				// var user_address_id = this.request.body.user_address_id;
				if (cart_id_list instanceof Array) {
					var ach_id = user.makeAchFromCartGoodsList(this.request.body);
					var ach = AchCon.get(ach_id);
					this.body = ResponObj("json", ach);
				} else {
					throwCusError("参数错误");
				}
			},
			//将单件商品转化为订单
			"/makeAchFromGoods": function() {
				var user = role.getUser(this);
				var goods_info = this.request.body;
				if (goods_info.goods_id) {
					var ach_id = user.makeAchFromGoods(goods_info);
					var ach = AchCon.get(ach_id);
					this.body = ResponObj("json", ach);
				} else {
					throwCusError("参数错误");
				}
			},
			//计算购物车中商品的总价格以及相关的优惠信息
			"/getCash": function() {
				var user = role.getUser(this);
				var cart_id_list = this.request.body.cart_id_list;
				// var user_address_id = this.request.body.user_address_id;
				if (cart_id_list instanceof Array) {
					console.log(this.request.body);
					var ach_id = user.makeAchFromCartGoodsList(this.request.body, true);
					var ach = AchCon.get(ach_id);
					ach.getCash();
					this.body = ResponObj("json", ach);
				} else {
					throwCusError("参数错误");
				}
			}
		},
		put: {
			"/updateGoodsNum": function() {
				var user = role.getUser(this);
				var body = this.request.body;
				var goods_id = body.goods_id;
				var num = body.num;
				var cart_id = body.cart_id;
				if (!(goods_id && num && cart_id)) {
					throwCusError("参数错误")
				}
				user.updateGoodsNumInCart(cart_id, goods_id, num);
				module.exports.get["/getByBus"].call(this);
			},
		},
		"delete": {
			"/remove_goods": function() {
				var user = role.getUser(this);
				var goods_id = this.request.body.goods_id;
				var cart_id = this.request.body.cart_id;
				if (!goods_id || !cart_id) {
					throwCusError("参数错误")
				}
				user.removeGoodsFromCart(cart_id, goods_id)
				var query = this.request.query;
				module.exports.get["/getByBus"].call(this);
			},
			"/all": function() {
				var user = role.getUser(this);
				user.clearCart();
				module.exports.get["/getByBus"].call(this);
			}
		}
	}
}());